package com.kly.cashmall.utils.system;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author zxp
 * @since 27/10/21
 */
public class MiuiUtils {
    public static final int REQUEST_CODE_SERVICE_SMS = 100;

    public MiuiUtils() {
    }

    public static void goPermissionSettings(Activity activity) {
        if (isMIUI()) {
            Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
            intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
            intent.putExtra("extra_pkgname", activity.getPackageName());
            if (isIntentAvailable(intent, activity)) {
                activity.startActivityForResult(intent, 100);
            } else {
                intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
                intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
                intent.putExtra("extra_pkgname", activity.getPackageName());
                if (isIntentAvailable(intent, activity)) {
                    activity.startActivityForResult(intent, 100);
                } else {
                    intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
                    Uri uri = Uri.fromParts("package", activity.getPackageName(), (String)null);
                    intent.setData(uri);
                    activity.startActivityForResult(intent, 100);
                }
            }
        }

    }

    public static boolean isMIUI() {
        return !TextUtils.isEmpty(getSystemProperty("ro.miui.ui.version.name"));
    }

    public static String getSystemProperty(String propName) {
        BufferedReader input = null;

        Object var4;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + propName);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            String line = input.readLine();
            input.close();
            return line;
        } catch (IOException var14) {
            Log.e("~~~", "Unable to read sysprop " + propName, var14);
            var4 = null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException var13) {
                    Log.e("~~~", "Exception while closing InputStream", var13);
                }
            }

        }

        return (String)var4;
    }

    public static boolean isIntentAvailable(Intent intent, Context context) {
        if (intent != null && context != null && context.getPackageManager() != null) {
            return context.getPackageManager().queryIntentActivities(intent, 65536).size() > 0;
        } else {
            return false;
        }
    }
}
